package fs.util {
	//import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	/**
	 * ...
	 * @author F.S.
	 */
	public final class SlideManager {
		private var _left:int;
		private var _top:int;
		private var _right:int;
		private var _bottom:int;
		private var _object:Sprite;
		private var _speed:Array;
		private var _old_point:Array;
		private var _x_move_fctr:Number;
		private var _y_move_fctr:Number;
		
		public function SlideManager(use_factory_methold:Object) {}
		public static function startListen(object:Sprite,
										left:int,top:int,right:int,bottom:int):void {
			var sm:SlideManager = new SlideManager(null);
			sm._left = left;
			sm._top = top;
			sm._right = right;
			sm._bottom = bottom;
			sm._object = object;
			
			sm._x_move_fctr = (sm._right == sm._left)?0:1;
			sm._y_move_fctr = (sm._top == sm._bottom)?0:1;
			
			sm._object.addEventListener(MouseEvent.MOUSE_DOWN, sm.on_mouse_down);
		}
		
		private function on_mouse_down(e:MouseEvent):void {
			_object.removeEventListener(Event.ENTER_FRAME, on_enter_frame);
			_object.removeEventListener(MouseEvent.MOUSE_DOWN, on_mouse_down);
			_object.stage.addEventListener(MouseEvent.MOUSE_MOVE, on_mouse_move);
			_object.stage.addEventListener(MouseEvent.MOUSE_UP, on_mouse_up);
			
			_old_point = [_object.stage.mouseX, _object.stage.mouseY];
			_speed = [0, 0];
		}
		
		private function on_enter_frame(e:Event):void {
			_speed[0] *= 0.9;
			_speed[1] *= 0.9;
												
			_object.x += _speed[0]*_x_move_fctr;
			_object.y += _speed[1]*_y_move_fctr;
			
			if (_object.x < _left) {
				_speed[0] = (_left - _object.x) / 10;
			}else if (_object.x > _right) {
				_speed[0] = (_right - _object.x) / 10;
			}
			
			if (_object.y < _top) {
				_speed[1] = (_top - _object.y) / 10;
			}else if (_object.y > _bottom) {
				_speed[1] = (_bottom - _object.y) / 10;
			}
			
			if (Math.abs(_speed[0]) < 0.5 && Math.abs(_speed[1]) < 0.5) {
				if (Math.abs(_object.x - _left) < 5) {
					_object.x = _left;
				}else if (Math.abs(_object.x - _right) < 5) {
					_object.x = _right;
				}
				if (Math.abs(_object.y - _top) < 5) {
					_object.y = _top;
				}else if (Math.abs(_object.y - _bottom) < 5) {
					_object.y = _bottom;
				}
				_object.removeEventListener(Event.ENTER_FRAME, on_enter_frame);
			}
		}
		
		private function on_mouse_up(e:MouseEvent):void {
			_object.addEventListener(MouseEvent.MOUSE_DOWN, on_mouse_down);
			_object.stage.removeEventListener(MouseEvent.MOUSE_MOVE, on_mouse_move);
			_object.stage.removeEventListener(MouseEvent.MOUSE_UP, on_mouse_up);
			_object.addEventListener(Event.ENTER_FRAME, on_enter_frame);
		}
		
		private function on_mouse_move(e:MouseEvent):void {
			_speed  = [_object.stage.mouseX - _old_point[0], 
												_object.stage.mouseY - _old_point[1]];
												
			_object.x += _speed[0] * _x_move_fctr;
			_object.y += _speed[1] * _y_move_fctr;
			
			_old_point = [_object.stage.mouseX, _object.stage.mouseY];
		}
		
		public function clear():void {
			_object.removeEventListener(Event.ENTER_FRAME, on_enter_frame);
			_object.removeEventListener(MouseEvent.MOUSE_DOWN, on_mouse_down);
			_object.stage.removeEventListener(MouseEvent.MOUSE_MOVE, on_mouse_move);
			_object.stage.removeEventListener(MouseEvent.MOUSE_UP, on_mouse_up);
		}
	}

}